using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ClassTree : AdminBaseUserControl
    {
        #region Properties
        public string SelectedValue
        {
            get { return Convert.ToString(ViewState["_ClassID"]); }
            set { ViewState["_ClassID"] = value; }
        }
        #endregion


        public event EventHandler SelectedClassChanged;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindTree();
            }
            string target = Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrEmpty(target))
            {
                if (target.Equals(this.tvClassType.UniqueID))
                {
                    if (ViewState["previousArg"] != null)
                    {
                        string previousArg = (string)ViewState["previousArg"];
                        if (previousArg.Equals(Request.Form["__EVENTARGUMENT"]))
                        {                            
                            if (SelectedClassChanged != null && tvClassType.SelectedNode.Depth == 2)
                            {
                                this.SelectedValue = tvClassType.SelectedValue;
                                SelectedClassChanged(this, new EventArgs());
                            }
                            ViewState["_currArg"] = Request["__EVENTARGUMENT"].ToString();
                        }
                    }
                    ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
                }
            }
        } 

        public void BindTree()
        {
            tvClassType.Nodes.Clear();
            TreeNode root = new TreeNode("Class Type","root");
            root.NavigateUrl = "javascript:void(0)";
            IList<ICmsClassType> list=ClassTypeController.GetClassTypeList();
            foreach(ICmsClassType cct in list)
            {
                TreeNode  parent =new TreeNode(cct.Name,cct.ID.ToString());
                parent.NavigateUrl = "javascript:void(0)";
                IList<ICmsClass> classList = ClassController.GetClassListByClassTypeID(cct.ID,true);
                foreach (ICmsClass cc in classList)
                {
                    parent.ChildNodes.Add(new TreeNode(cc.Name,cc.ID.ToString()));
                }
                root.ChildNodes.Add(parent);
               
            }
            tvClassType.Nodes.Add(root);
            tvClassType.ExpandAll();
            
        }

        public void BindRefesh(object sender, EventArgs e)
        {
            BindTree();
        }

        public void DoDeleteClass(object sender, EventArgs e)
        {
            if( tvClassType.SelectedNode.Depth == 2)
            {
                long value = Convert.ToInt64(this.SelectedValue);
                DeleteNode(value);
            }            
            
        }

        private void DeleteNode(long value)
        {
            ClassController.DeleteClass(value);
            BindTree();
        }
        protected void tvClassType_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView tvClassType=(TreeView)sender;
            if (SelectedClassChanged != null && tvClassType.SelectedNode.Depth==2)
            {
                this.SelectedValue = tvClassType.SelectedValue;
                SelectedClassChanged(this, new EventArgs());
            }
            ViewState["_currArg"] = Request["__EVENTARGUMENT"].ToString();

        }
        public void TestEvent(object sender, EventArgs e)
        {

        }
    }
}